package com.example.localmusic;

import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //歌曲列表
    private List<SongBean> list;
    //设配器
    private MusicAdapter musicAdapter;
    //RecyclerView列表
    private RecyclerView recyclerView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });


        //初始化歌曲列表
        list = new ArrayList<>();

        //初始化RecyclerView对象
        recyclerView = findViewById(R.id.musicListView);

        //初始化适配器
        musicAdapter = new MusicAdapter(this,list);
        //设置适配器
        recyclerView.setAdapter(musicAdapter);

        //布局管理
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false);
        recyclerView.setLayoutManager(linearLayoutManager);

        //获取音乐数据
        getMusic();

    }

    /**
     * 获取音乐
     */
    private void getMusic(){
        ContentResolver contentResolver = getContentResolver();
        //获取本地音乐Uri
        Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        //查询音乐数据
        Cursor cursor = contentResolver.query(uri, null, null, null, null);
        //遍历cursor对象
        Log.i("歌曲", String.valueOf(cursor));
        //初始化id
        int id = 0;
        while (cursor.moveToNext()){
            id++;
            String index = String.valueOf(id);//int转为字符串类型
            //获取歌曲名字
            String songName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            //获取歌手名字
            String singer = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));

            //获取歌曲时长，并且转为指定格式
            long longSongTime = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("mm:ss");
            String songTime = simpleDateFormat.format(new Date(longSongTime));

            //初始化Song对象
            SongBean songBean = new SongBean(index+"：", songName+"("+singer+")", songTime);

            //将Song对象添加到列表
            list.add(songBean);
        }
        //关闭游标
        cursor.close();

        //数据变化，提示适配器更新
        musicAdapter.notifyDataSetChanged();

    }
}